package com.laborder.sysyy.controller.teacher;


import com.laborder.sysyy.entity.College;
import com.laborder.sysyy.entity.Teacher;
import com.laborder.sysyy.service.college.CollegeService;
import com.laborder.sysyy.service.teacher.TeacherService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "teacher")
public class TeacherController {
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private CollegeService collegeService;

	@GetMapping("toTeacher")
	public String toTeacher(Model model){
		List<College> c = collegeService.selectCollege();
		model.addAttribute("collegeList",c);
		return "/teacher/teacherList";
	}
	@GetMapping("toAddTeacher")
	public String toAddTeacher(Model model){
		List<Teacher> t = teacherService.getAllTeacher();
		List<College> c = collegeService.selectCollege();
		model.addAttribute("teacherList",t);
		model.addAttribute("collegeList",c);
		return "/teacher/teacherAdd";
	}
	 /**
	   * @function: 教师分页查询
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/8 15:16
	   */
	 @ResponseBody
	@RequestMapping("/findTeacherWithPage")
	public Map<String,Object> findTeacherWithPage(int page, int limit,String clName,String tName){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("page",(page-1)*limit);
		map.put("limit",limit);
		map.put("clName",clName);
		 map.put("tName",tName);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",teacherService.getTeacherCount(map));
		resultMap.put("data",teacherService.findTeacherWithPage(map));
		return  resultMap;
	}

	@RequestMapping("/getTeacherInfo")
	public Teacher getTeacherInfo(int tId) {
		return teacherService.getTeacherInfo(tId);
	}

	 /**
	   * @function: 添加教师信息
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/9 21:42
	   */
	@ResponseBody
	@RequestMapping("saveTeacherInfo")
	public  int saveTeacherInfo(Teacher teacher) {
		int flag = 0;
		Teacher t =teacherService.getTeacherInfoByName(teacher.gettName(),teacher.gettPhone());
		if(t!=null){
			flag = -1;
		}else if(t==null){
			flag = teacherService.saveTeacherInfo(teacher);
		}else{
			flag = 0;
		}
		return flag;
	}
	 /**
	   * @function: 删除教师信息
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/10 14:12
	   */
	 @ResponseBody
	@RequestMapping("/delTeacherInfo")
	public  boolean delTeacherInfo(int tId) {
		boolean flag = false;
		if(!flag){
			flag = teacherService.delTeacherInfo(tId);
		}
		return flag;
	}


	/**
	 * @function: 批量删除教师信息
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:12
	 */
	@ResponseBody
	@RequestMapping("/delManyTeacherInfo")
	public  boolean delManyTeacherInfo( @RequestParam("tId[]") int tId[]  ) {
		boolean flag = false;
		for(Integer tIds : tId){
		     flag  = teacherService.delManyTeacherInfo(tIds);
		}
		return flag;
	}
	 /**
	   * @function:  教师信息查看
	   * @author: 759057893@qq.com lyz
	   * @date: 2019/1/10 14:45
	   */
	 @RequestMapping("teacherDetail")
	public String teacherDetail(Model model,int tId) {
		Teacher teacher = teacherService.selectTeacherById(tId);
		 List<College> c = collegeService.selectCollege();
		 model.addAttribute("collegeList",c);
		  model.addAttribute("teacher", teacher);
		  return "teacher/teacherDetail";
	}
	/**
	 * @function:  教师信息修改前回显
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 14:45
	 */
	@RequestMapping("editTeacher")
	public  String editTeacher(Model model,int tId){
		Teacher teacher = teacherService.selectTeacherById(tId);
		model.addAttribute("teacher", teacher);
		List<College> c = collegeService.selectCollege();
		model.addAttribute("collegeList",c);
		return "teacher/editTeacher";
	}

	/**
	 * @function: 修改教师数据
	 * @author: 759057893@qq.com lyz
	 * @date: 2019/1/10 16:00
	 */
	@ResponseBody
	@RequestMapping("/updateTeacherInfo")
	public  boolean updateTeacherInfo(Teacher teacher) {

		return teacherService.updateTeacherInfo(teacher);
	}

}